import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * 描述:
 *
 * @author yanzhengwu
 * @create 2020-12-29 14:06
 */
public class Excel {

    public static void main(String[] args) throws IOException {
        InputStream is = new FileInputStream("F:htmlToExcel/text.xlsx");
        Workbook workbook = new XSSFWorkbook(is);
        CTWorkbook ctWorkbook = ((XSSFWorkbook) workbook).getCTWorkbook();

        Sheet sheet1 = workbook.getSheetAt(0);
        for (int i = 0; i < sheet1.getPhysicalNumberOfRows(); i++) {
            Row row = sheet1.getRow(i);
            for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
                Cell cell = row.getCell(j);
                if (null != cell && !"".equals(cell.getStringCellValue())) {
                    System.out.println("第" + (i + 1) + "行" + "第" + (j + 1) + "列" + cell);
                }

            }
            //System.out.println();
        }

        //for (Row row : sheet1) {
            //for (Cell cell : row) {
                int sheetMergeCount = sheet1.getNumMergedRegions();
                for (int i = 0; i < sheetMergeCount; i++) {
                    CellRangeAddress range = sheet1.getMergedRegion(i);

                    int firstRow = range.getFirstRow();
                    int lastRow = range.getLastRow();
                    int firstColumn = range.getFirstColumn();
                    int lastColumn = range.getLastColumn();
                    //if (row.getRowNum() >= firstRow && row.getRowNum() <= lastRow) {
                        //if (cell.getColumnIndex() >= firstColumn && cell.getColumnIndex() <= lastColumn) {
                            System.out.println("合并的值：" + sheet1.getRow(firstRow).getCell(firstColumn));
                    System.out.println("firstRow=>" + firstRow);
                    System.out.println("lastRow=>" + lastRow);
                    System.out.println("firstColumn=>" + firstColumn);
                    System.out.println("lastColumn=>" + lastColumn);
                            System.out.println();
                        //}
                    //}
                //}
            //}
        }


        is.close();
    }
}
